<!-- 首页 -->
<template>
	<view>
	    <view class="topic-header">
	    	<view class="topic-title">推荐话题</view>
	    	<view class="topic-more" @click="notice">
				<view class="bell">
					<u-icon name="bell"></u-icon>
					<view class="tab-btn-dot" v-if="notice_num > 0">{{notice_num}}</view>
				</view>
			</view>
	    </view>
		<topic-scroll @openTopic="openTopic" :dataList="hot_topicList"></topic-scroll>
	    <talk-list @menu="menu" @addParty="addParty" @thumb="thumb" @chat="chat" :dataList="dataList"></talk-list>
		<view class="loadmore-box" v-if="dataList.length > 0">
			<u-loadmore :status="status" @loadmore="load"></u-loadmore>
		</view>
		<u-back-top :scrollTop="scrollTop"></u-back-top>
	</view>
</template>

<script>
import {mapState} from 'vuex'
export default {
	props:{
		scrollTop: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default(){
				return 0
			}
		},
	},
	data() {
		return {
			status: 'loadmore',
			isEnd: false,
			current: 1,
			pageSize: 20,
			dataList: [],
			list: [],
		};
	},
	computed: {
		...mapState(['vuex_user','hot_topicList','notice_num']),
	},
	methods: {
		init() {
			this.getHotTopicList();
			this.query();
		},
		query() {
			this.current = 1;
			this.isEnd = false;
			this.status = 'loadmore';
			this.loadquery();
		},
		loadquery() {
			var userId = uni.getStorageSync("userId");
			if(userId){
				this.judgeLogin(() => {
					
				});
			}
			this.$u.get('/wq/dynamic/list',{
				pageNum: this.current,
				appId: this.website.appId,
				pageSize: this.pageSize,
				userId: userId,
				lon: this.vuex_user.lon,
				lat: this.vuex_user.lat,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = res.rows;
			}).catch(err => {
				
			});
		},
		load() {
			this.loadmore();
		},
		loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			
			var userId = uni.getStorageSync("userId");
			this.$u.get('/wq/dynamic/list',{
				pageNum: this.current,
				appId: this.website.appId,
				pageSize: this.pageSize,
				userId: userId,
				lon: this.vuex_user.lon,
				lat: this.vuex_user.lat,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = this.dataList.concat(res.rows);
			})
		},
		chat(e) {
			if(e.isParty == '2'){
				this.$u.route({
					url: 'packgeMine/pages/game/dynamic',
					params: {
						dynamicId: e.id,
						back: true,
						my: 'true',
					}
				});
				return;
			}
			this.$u.route({
				url: 'packgeMine/pages/index/dynamic',
				params: {
					dynamicId: e.id,
				}
			});
		},
		addParty(e,run) {
			this.judgeLogin(() => {
				this.$u.post('/wq/dynamic/addParty',{
					id: e.id,
				}).then(res => {
					this.$u.toast('领取成功');
					run(true);
				});
			});
		},
		getVotoList() {
			this.$u.get('/wq/vote/list',{
				pageNum: this.current,
				pageSize: this.pageSize,
			}).then(res => {
				var rows = res.rows;
				for(var i=0;i<rows.length;i++){
					var row = rows[i];
					row.image = row.image.split(',')[0];
				}
				this.list = rows;
			})
		},
		getHotTopicList() {
			var userId = uni.getStorageSync("userId");
			this.$u.get('/wq/topic/getHotTopic',{
				appId: this.website.appId,
				userId: userId,
			}).then(res => {
				this.$store.commit('setHotTopicList', res.data);
			});
		},
		detail(index) {
			var voteId = this.list[index].id;
			this.$u.route({
				url: 'packgeMine/pages/vote/game/detail',
				params: {
					id: voteId,
				}
			});
		},
		notice() {
			this.judgeLogin((res) => {
				this.$u.route({
					url: 'packgeMine/pages/index/notice',
				});
			});
		},
		test(data, e) {
			var that = this;
			var tmplId = data.tmplIds[0];
			wx.getSetting({
			  withSubscriptions: true,
			  success(res) {
				console.log(res)
				if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
				  if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
					let item = res.subscriptionsSetting.itemSettings[tmplId]
					if (item == "reject") {
					  uni.showModal({
					  	title: '温馨提示',
					  	content: '请打开通知管理里面的站内信提醒',
					  	showCancel: false,
					  	success: function(res) {
					  		uni.openSetting({
							  success(res) {
								console.log(res.authSetting)
							  },
							  fail(err) {
								console.error(err);
							  }
							});
					  	}
					  });
					} else if (item == "accept") {
					  that.subscribeMessage(data,e);
					} else if (item == "ban") {
					  uni.showModal({
					  	title: '温馨提示',
					  	content: '请在设置打开订阅消息开关',
					  	showCancel: false,
					  	success: function(res) {
					  		
					  	}
					  });
					}
				  }else{
					that.tipMsg(data, e);
				  }
				} else {
				  uni.showModal({
				  	title: '温馨提示',
				  	content: '请在设置打开通知管理开关',
				  	showCancel: false,
				  	success: function(res) {
				  		uni.openSetting({
						  success(res) {
							console.log(res.authSetting)
						  },
						  fail(err) {
							console.error(err);
						  }
						});
				  	}
				  });
				}
			  }
			});
		},
		tipMsg(data, e) {
			var that = this;
			uni.showModal({
				title: '温馨提示',
				content: '请勾选总是保持以上选择',
				showCancel: false,
				success: function(res) {
					that.subscribeMessage(data,e);
				}
			});
		},
		subscribeMessage(data,e) {
			var _this = this;
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: data.tmplIds,
				success: function(res) {
					var code = res[data.tmplIds[0]];
					if('reject' == code){
						_this.$u.toast('请点击允许订阅消息');
					}else{
						_this.$u.route({
							url: 'packgeMine/pages/index/notice',
						});
					}
				},
				fail: function(res) {
					_this.$u.toast('订阅失败');
				},
				complete: (res) => {
					
				}
			});
		},
		clickSheet(sheet) {
			if('remove' == sheet.tag){
				this.deleteTopic(sheet.id);
			}else if('jubao' == sheet.tag){
				this.addBlack(sheet.userId);
			}else if('lahei' == sheet.tag){
				this.addBlackUser(sheet.userId);
			}
		},
		addBlack(userId) {
			this.$u.post('/system/user/addBlack',{
				userId: userId,
			}).then(res => {
				this.$u.toast('举报成功');
				//this.query();
			});
		},
		addBlackUser(userId) {
			this.$u.post('/system/user/addBlackUser',{
				userId: userId,
			}).then(res => {
				this.$u.toast('拉黑成功');
				//this.query();
			});
		},
		deleteTopic(id) {
			this.$u.get('/wq/dynamic/delete',{
				id: id,
			}).then(res => {
				this.query();
			});
		},
		openTopic(topic) {
			this.judgeLogin(() => {
				this.$u.route({
					url: 'pages/topic/index',
					params: {
						topicId: topic.id,
					}
				});
			});
		},
		thumb(e,i) {
			this.$u.post('/wq/dynamicGive/add',{
				dynamicId: e.id,
			}).then(res => {
				if(res.data){
					this.dataList[i].giveNum = this.dataList[i].giveNum + 1;
				}else{
					this.dataList[i].giveNum = this.dataList[i].giveNum - 1;
				}
			});
		},
		menu(e) {
			this.$emit('menu', e);
		},
		more() {
			this.$u.route({
				url: 'packgeMine/pages/vote/game/index',
			});
		},
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.topic-header {
		padding: 15px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.topic-title {
		font-size: 38rpx;
		font-weight: bolder;
	}
	.topic-more {
		font-size: 38rpx;
		color: #fff;
		background-color: #e3048f;
		padding: 6rpx 20rpx;
		border-radius: 20px;
		display: flex;
		align-items: center;
	}
	.wrap-box {
		padding: 0 15px;
	}
	.topic-right {
		display: flex;
		align-items: center;
	}
	.top-info {
		margin-right: 5px;
		color: #505152bd;
		font-size: 24rpx;
	}
	.loadmore-box{
		padding: 15px;
		padding-bottom: 70px;
	}
	.bell{
		position: relative;
	}
	.tab-btn-dot {
		background-color: #fa8206;
		position: absolute;
		top: -10rpx;
		right: -10px;
		z-index: 9;
		border-radius: 100%;
		font-size: 6px;
		padding: 15rpx;
		color: #fff;
		height: 10rpx;
		width: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
